<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <span>用户名</span>
    <!-- 1.使用v-model 将值同步到 data 中 -->
    <input type="text" v-model.lazy.trim='uname'>

    <span v-text='tip'></span>
  </div>
  <script src="./js/vue.js"></script>
  <script>
    var vm = new Vue({
      el: '#app',
      data: {
        uname: '',// 输入的值
        tip: ''// 用户提示信息
      },
      methods: {
        // 3.处理验证的逻辑
        validUname(uname) {
          setTimeout(() => {
            if (uname === 'admin') {
              this.tip = '用户名以存在，请换一个'
            } else {
              this.tip = '用户名可以使用'
            }
          }, 1000)
        }
      },
      watch: {
        // 2.对用户输入的值进行监听
        uname: function (uname) {
          this.tip = '正在验证中.........'
          this.validUname(uname)
        }
      }
    })
  </script>
</body>

</html>